<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <!-- 根组件不显示图标 -->
    <el-col v-if="isRoot">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <!-- 公司组件显示图标 -->
    <el-col v-else>
      <i class="el-icon-office-building" />
      <strong> {{ nodeData.name }}</strong>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col class="optBtn">
          <el-dropdown>
            <span>
              操作
              <i class="el-icon-arrow-down" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addDepartment">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="isRoot" @click.native="editDepartment">编辑部门</el-dropdown-item>
              <el-dropdown-item
                v-if="isRoot"
                @click.native="delDepartment"
              >删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentById } from '@/api/departments'
export default {
  props: {
    // 来自每一行的数据
    nodeData: {
      type: Object,
      required: true
    },
    // 是否根组件 不显示图标 显示编辑和删除部门
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    //   删除部门
    async delDepartment() {
      try {
        //  1 询问是否真的要删除
        await this.$confirm('确定要删除吗？')
        //  2 调用接口删除数据
        await delDepartmentById(this.nodeData.id)
        //  3 把事件响应给父组件 更新数据
        this.$emit('delDepartmentById')
      } catch (error) {
        console.log(error)
      }
    },
    // 添加部门
    addDepartment() {
      // 响应事件 回传当前部门数据
      this.$emit('addDepartment', this.nodeData)
    },
    // 编辑部门
    editDepartment() {
      this.$emit('editDepartment', this.nodeData)
    }
  }
}
</script>

<style>
</style>
